<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basePackage}.dao.${table.className}Dao">

    <sql id="Base_Column_List">
        <trim suffixOverrides=",">
        <#list table.columns as column>
            <#if !column.isDeletedColumn>
            ${column.columnName},
            </#if>
        </#list>
        </trim>
    </sql>

    <insert id="insert" parameterType="${basePackage}.entity.${table.className}" useGeneratedKeys="true"
            keyProperty="id">
        insert into ${table.tableName}(
        <trim suffixOverrides=",">
        <#list table.columns as column>
            <#if (!column.isPrimaryKeyColumn
                  && !column.isCreateTimeColumn
                  && !column.isUpdateTimeColumn
                  && !column.isDeletedColumn)>
            ${column.columnName},
            </#if>
        </#list>
        </trim>
        )
        values (
        <trim suffixOverrides=",">
        <#list table.columns as column>
            <#if (!column.isPrimaryKeyColumn
                  && !column.isCreateTimeColumn
                  && !column.isUpdateTimeColumn
                  && !column.isDeletedColumn)>
            ${r"#{"}${column.fieldName}${r"}"},
            </#if>
        </#list>
        </trim>
        )
    </insert>

    <update id="update" parameterType="${basePackage}.entity.${table.className}">
        update ${table.tableName}
        <set>
        <#list table.columns as column>
             <#if (!column.isPrimaryKeyColumn
                   && !column.isCreateTimeColumn
                   && !column.isUpdateTimeColumn
                   && !column.isDeletedColumn)>
             ${column.columnName} = ${r"#{"}${column.fieldName}${r"}"},
             </#if>
        </#list>
        </set>
        where ${table.primaryKeyColumn.columnName} = ${r"#{"}${table.primaryKeyColumn.fieldName}${r"}"}
    </update>

    <update id="updateSelective" parameterType="${basePackage}.entity.${table.className}">
        update ${table.tableName}
        <set>
            <#list table.columns as column>
                <#if (!column.isPrimaryKeyColumn
                      && !column.isCreateTimeColumn
                      && !column.isUpdateTimeColumn
                      && !column.isDeletedColumn)>
            <if test="${column.fieldName} != null">
                ${column.columnName} = ${r"#{"}${column.fieldName}${r"}"},
            </if>
                </#if>
            </#list>
        </set>
        where ${table.primaryKeyColumn.columnName} = ${r"#{"}${table.primaryKeyColumn.fieldName}${r"}"}
    </update>

    <#if table.deletedColumn??>
    <update id="delete">
        update ${table.tableName} set ${table.deletedColumn.columnName} = REPLACE(unix_timestamp(now(3)),'.','')
        where ${table.primaryKeyColumn.columnName} = ${r"#{"}${table.primaryKeyColumn.fieldName}${r"}"}
    </update>
    <#else>
    <delete id="delete">
        delete from ${table.tableName}
        where ${table.primaryKeyColumn.columnName} = ${r"#{"}${table.primaryKeyColumn.fieldName}${r"}"}
    </delete>
    </#if>

    <select id="getById" resultType="${basePackage}.entity.${table.className}">
        select
        <include refid="Base_Column_List"/>
        from ${table.tableName}
        <where>
            ${table.primaryKeyColumn.columnName} = ${r"#{"}id${r"}"}
        </where>
    </select>

    <select id="getListByIds" resultType="${basePackage}.entity.${table.className}">
        select
        <include refid="Base_Column_List"/>
        from ${table.tableName}
        <where>
            ${table.primaryKeyColumn.columnName} in
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                ${r"#{"}id${r"}"}
            </foreach>
        </where>
    </select>

    <select id="count" resultType="int">
        select
        count(*)
        from ${table.tableName}
        <where>
            1 = 1
            <foreach item="condition" collection="conditions">
                and ${r"${"}condition.field${r"}"} ${r"${"}condition.op${r"}"} ${r"#{"}condition.value${r"}"}
            </foreach>
            <#if table.deletedColumn??>
                and ${table.deletedColumn.columnName} = 0
            </#if>
        </where>
    </select>

    <select id="list" resultType="${basePackage}.entity.${table.className}">
        select
        <include refid="Base_Column_List"/>
        from ${table.tableName}
        <where>
            1 = 1
            <foreach item="condition" collection="conditions">
                and ${r"${"}condition.field${r"}"} ${r"${"}condition.op${r"}"} ${r"#{"}condition.value${r"}"}
            </foreach>
            <#if table.deletedColumn??>
                and ${table.deletedColumn.columnName} = 0
            </#if>
        </where>
    </select>

    <select id="page" resultType="${basePackage}.entity.${table.className}">
        select
        <include refid="Base_Column_List"/>
        from ${table.tableName}
        <where>
            1 = 1
            <foreach item="condition" collection="conditions">
                and ${r"${"}condition.field${r"}"} ${r"${"}condition.op${r"}"} ${r"#{"}condition.value${r"}"}
            </foreach>
            <#if table.deletedColumn??>
                and ${table.deletedColumn.columnName} = 0
            </#if>
        </where>
    </select>

</mapper>